﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace wpNewsList.wpMain
{
    [ToolboxItemAttribute(false)]
    public class wpMain : WebPart
    {

        string mySSite = "http://sp.korablik.local";
        string mySWeb = "/";
        string mySList = "Новости";
        string mySTitle = "Title";
        string mySRem = "Описание";
        int myIRowCount = 0;
        string mySOrder = "Created";
        string mySUrlView = "/_layouts/spSiteNewsView/NewsView.aspx";
        string mySAnince = "Анонс";
        //--
        Panel mainPanel = null;
        //--
        [WebBrowsable(true), WebDisplayName("Поле для сортировки"), WebDescription("Поле для сортировки"),
        Personalizable(PersonalizationScope.Shared), Category("(Ko) Settings")]
        public String SOrder
        {
            get { return mySOrder; }
            set { mySOrder = value; }
        }
        //--
        [WebBrowsable(true), WebDisplayName("Site"), WebDescription("Site"),
        Personalizable(PersonalizationScope.Shared), Category("(Ko) Settings")]
        public String SSite
        {
            get { return mySSite; }
            set { mySSite = value; }
        }
        //--
        [WebBrowsable(true), WebDisplayName("Web"), WebDescription("Web"),
        Personalizable(PersonalizationScope.Shared), Category("(Ko) Settings")]
        public String SWeb
        {
            get { return mySWeb; }
            set { mySWeb = value; }
        }
        //--
        [WebBrowsable(true), WebDisplayName("Библиотека"), WebDescription("Библиотека"),
        Personalizable(PersonalizationScope.Shared), Category("(Ko) Settings")]
        public String SList
        {
            get { return mySList; }
            set { mySList = value; }
        }
        //--
        [WebBrowsable(true), WebDisplayName("Заголовок"), WebDescription("Заголовок"),
        Personalizable(PersonalizationScope.Shared), Category("(Ko) Settings")]
        public String STitle
        {
            get { return mySTitle; }
            set { mySTitle = value; }
        }
        //--
        [WebBrowsable(true), WebDisplayName("Описание"), WebDescription("Описание"),
        Personalizable(PersonalizationScope.Shared), Category("(Ko) Settings")]
        public String SRem
        {
            get { return mySRem; }
            set { mySRem = value; }
        }
        //--
        [WebBrowsable(true), WebDisplayName("Кол-во новостей"), WebDescription("Кол-во новостей отображаемых на виджете (0 - все)"),
        Personalizable(PersonalizationScope.Shared), Category("(Ko) Settings")]
        public int IRowCount
        {
            get { return myIRowCount; }
            set { myIRowCount = value; }
        }
        //--
        [WebBrowsable(true), WebDisplayName("Адрес страницы просмотра"), WebDescription("Адрес страницы просмотра"),
        Personalizable(PersonalizationScope.Shared), Category("(Ko) Settings")]
        public string SUrlView
        {
            get { return mySUrlView; }
            set { mySUrlView = value; }
        }
        //--
        [WebBrowsable(true), WebDisplayName("Поле анонса"), WebDescription("Поле анонса для отображения"),
        Personalizable(PersonalizationScope.Shared), Category("(Ko) Settings")]
        public string SAnince
        {
            get { return mySAnince; }
            set { mySAnince = value; }
        }
        //--
        protected override void CreateChildControls()
        {
            CreateList();
        }

        public void CreateList()
        {
            if (mainPanel != null) mainPanel.Dispose();
            mainPanel = new Panel();
            //--
            SPSite site = new SPSite(mySSite);
            SPWeb mWeb = site.OpenWeb(mySWeb);
            try
            {
                //--
                Table mTab = new Table();
                //mTab.BorderStyle = BorderStyle.Dashed;
                mTab.BorderWidth = 0;
                //mTab.Style.Add("padding", "4px");
                //--
                TableRow mRow;
                TableCell mCell;
                //--
                mWeb.AllowUnsafeUpdates = true;
                SPList list = mWeb.Lists[mySList];
                SPQuery query = new SPQuery();
                query.RowLimit = (uint)myIRowCount;
                query.Query = "<OrderBy><FieldRef Name='"+mySOrder+"' Ascending='False' /></OrderBy>";

                foreach (SPListItem item in list.GetItems(query))
                {
                    //--
                    mRow = new TableRow();
                    //--
                    mCell = new TableCell();
                    mCell.Style.Add("padding", "4px");
                    mCell.HorizontalAlign = HorizontalAlign.Right;
                    mCell.Width = 110;
                    SPAttachmentCollection aColl = item.Attachments;
                    if (aColl != null)
                    {
                        for (int i = 0; i < aColl.Count; i++)
                        {
                            Image im = new Image();
                            im.BorderWidth = 0;
                            //im.BorderColor = 
                            im.ImageUrl = aColl.UrlPrefix + aColl[i];
                            im.Width = 100;
                            im.Height = 100;
                            mCell.Controls.Add(im);
                        }
                    }
                    mRow.Cells.Add(mCell);
                    //--
                    mCell = new TableCell();
                    mCell.Style.Add("padding", "4px");
                    mCell.HorizontalAlign = HorizontalAlign.Left;
                    mCell.VerticalAlign = VerticalAlign.Top;
                    //mCell.Text = item[mySTitle].ToString(); //  + item["Author"].ToString()
                    //--
                    Panel p1 = new Panel();
                    Label l1 = new Label();
                    l1.Text = item["Created"].ToString();
                    p1.Controls.Add(l1);
                    mCell.Controls.Add(p1);
                    //--

                    //--
                    if (item[mySTitle] != null)
                    {
                        HyperLink hl = new HyperLink();
                        hl.Text = item[mySTitle].ToString();
                        string sForm = list.Forms[0].Url;
                        hl.NavigateUrl = site.Url + mySUrlView + "?site=" + mySSite + "&web=" + mySWeb + "&list=" + mySList + "&id=" + item.ID.ToString() + "&title=" + mySTitle + "&rem=" + mySRem;
                        mCell.Controls.Add(hl);
                    }
                    //--
                    if (item[mySAnince] != null)
                    {
                        Panel p2 = new Panel();
                        Label l2 = new Label();
                        l2.Text = item[mySAnince].ToString();
                        p2.Controls.Add(l2);
                        mCell.Controls.Add(p2);
                    }
                    //--
                    mRow.Cells.Add(mCell);
                    //--
                    mTab.Rows.Add(mRow);
                    mainPanel.Controls.Add(mTab);
                }
            }
            catch
            {
            }
            mWeb.Dispose();
            site.Dispose();
            //--
            this.Controls.Add(mainPanel);
        }
    }
}
